package com.bosssoft.hr.jobmonitor.config;


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.sqlite.SQLiteDataSource;

import javax.sql.DataSource;

/***
 * @Author lys(cherish)
 * @Description  mybatis配置
 * @Date  2019/2/26
 **/
@Configuration
public class MyBatisConfig {

    //数据源配置对象
    @Autowired
    private DataSourceProperties dataSourceProperties;

    /***
     * @Author lys(cherish)
     * @Description 数据源spring
     * @Date  2019/2/26
     * @Param []
     * @return javax.sql.DataSource
     **/
    @Bean(name="dataSource")
    public DataSource dataSource() {
        SQLiteDataSource dataSource = new SQLiteDataSource();
        dataSource.setUrl(dataSourceProperties.getUrl());
        return dataSource;
    }

    /***
     * @Author lys(cherish)
     * @Description sqlSession工厂
     * @Date  2019/2/26
     * @Param []
     * @return org.apache.ibatis.session.SqlSessionFactory
     **/
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        return sqlSessionFactoryBean.getObject();
    }

}
